package com.settlement.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.settlement.system.common.enums.PayEnum;
import com.settlement.system.common.util.DateUtils;
import com.settlement.system.mapper.SysPayOrderMapper;
import com.settlement.system.model.entity.SysPayOrder;
import com.settlement.system.model.form.SysPayOrderForm;
import com.settlement.system.model.query.SysPayOrderQuery;
import com.settlement.system.model.vo.SysPayOrderImportVo;
import com.settlement.system.service.SysPayOrderService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户订单表 服务实现类
 * </p>
 *
 * @author huangrensen
 * @since 2023-12-30
 */
@Service
public class SysPayOrderServiceImpl extends ServiceImpl<SysPayOrderMapper, SysPayOrder> implements SysPayOrderService {

    @Override
    public Page<SysPayOrder> getPage(SysPayOrderQuery queryParams) {
        // 查询参数
        int pageNum = queryParams.getPageNum();
        int pageSize = queryParams.getPageSize();
        // 查询数据
        Page<SysPayOrder> page = new Page<>(pageNum, pageSize);
        if (queryParams.getEndTime() != null) {
            queryParams.setEndTime(queryParams.getEndTime().plusDays(1));
        }
        Page<SysPayOrder> orderPage = this.baseMapper.getPage(page, queryParams);
        if (orderPage.getRecords().size() > 0) {
            for (SysPayOrder record : orderPage.getRecords()) {
                if (PayEnum.N.getValue().equals(record.getPayStatus())) {
                    record.setPayStatusName(PayEnum.N.getLabel());
                }else{
                    record.setPayStatusName(PayEnum.Y.getLabel());
                }
            }
        }
        return orderPage;
    }

    @Override
    public boolean saveSysPayOrder(SysPayOrderForm sysPayOrderForm) {
        SysPayOrder sysPayOrder = new SysPayOrder();
        BeanUtil.copyProperties(sysPayOrderForm, sysPayOrder);
        sysPayOrder.setCreateTime(null);
        return this.saveOrUpdate(sysPayOrder);
    }

    @Override
    public SysPayOrder saveSysPayOrder(SysPayOrder sysPayOrder) {
        this.saveOrUpdate(sysPayOrder);
        return sysPayOrder;
    }

    @Override
    public boolean checkOrderFinish(String orderNo) {
        SysPayOrder payOrder = this.baseMapper.selectOne(new LambdaQueryWrapper<SysPayOrder>().eq(SysPayOrder::getOrderNo, orderNo));
        Assert.isTrue(payOrder != null, "根据订单编号查找不到记录");
        if(PayEnum.Y.getValue().equals(payOrder.getPayStatus())){
            return true;
        }
        return false;
    }

    @Override
    public SysPayOrderForm getSysPayOrderFormData(Long id) {
        SysPayOrder sysPayOrder = this.getById(id);
        SysPayOrderForm sysPayOrderForm = new SysPayOrderForm();
        BeanUtil.copyProperties(sysPayOrder, sysPayOrderForm);
        return sysPayOrderForm;
    }

    @Override
    public boolean deleteSysPayOrder(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的数据为空");
        List<Long> idArray = Arrays.stream(ids.split(",")).map(Long::parseLong).collect(Collectors.toList());
        return this.removeByIds(idArray);
    }

    @Override
    public void importData(List<SysPayOrderImportVo> voList) {
        List<SysPayOrder> sysPayOrderList = new ArrayList<>();
        for (SysPayOrderImportVo importVo : voList) {
            SysPayOrder sysPayOrder = new SysPayOrder();
            BeanUtil.copyProperties(importVo, sysPayOrder);
            sysPayOrderList.add(sysPayOrder);
        }
        this.saveOrUpdateBatch(sysPayOrderList);
    }

    @Override
    public List<SysPayOrderImportVo> findByIds(String idsStr) {
        if (StrUtil.isBlank(idsStr)) {
            return new ArrayList<>();
        }
        List<Long> ids = Arrays.stream(idsStr.split(",")).map(Long::parseLong).collect(Collectors.toList());
        List<SysPayOrder> sysPayOrderList = this.getBaseMapper().selectBatchIds(ids);
        List<SysPayOrderImportVo> voList = new ArrayList<>();
        for (SysPayOrder sysPayOrder : sysPayOrderList) {
            SysPayOrderImportVo vo = new SysPayOrderImportVo();
            BeanUtil.copyProperties(sysPayOrder, vo);
            voList.add(vo);
        }
        return voList;
    }

    @Override
    public boolean paySuccess(String outTradeNo, String payNo, String time) {
        SysPayOrder payOrder = this.baseMapper.selectOne(new LambdaQueryWrapper<SysPayOrder>().eq(SysPayOrder::getOrderNo, outTradeNo));
        if (payOrder != null) {
            payOrder.setPayNo(payNo);
            payOrder.setPayTime(DateUtils.convertStringToLocalDateTime(time));
            payOrder.setPayStatus(PayEnum.Y.getValue());
            return this.saveOrUpdate(payOrder);
        }
        return false;
    }
}
